//
//  MapGroupsDemoViewController.m
//  FMDemoBaseMap
//
//  Created by fengmap on 16/10/17.
//  Copyright © 2016年 fengmap. All rights reserved.
//
//  楼层显示控制
//  简介：地图单、多楼层显示设置；楼层切换；

#import "MapGroupsDemoViewController.h"
#import "FMMapKit.h"

@interface MapGroupsDemoViewController () <FMKMapViewDelegate>
{
    FMKMapView *_mapView;
    /// 是否显示全部楼层(多层显示)
    BOOL _isTotal;
    /// 当前楼层/当前焦点层 （单层显示时表示当前展示的楼层，多层显示时表示当前展示的焦点层），可以从_mapView.getFocusGroupID查询
    NSString *_currentGroup;
    
    UIButton *_selectGroupBtn;
}

@end

@implementation MapGroupsDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 离线地图加载
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"jlai-cyds-museum02" ofType:@"fmap"];
    _mapView = [[FMKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) path:dataPath delegate:self];
    [self.view addSubview:_mapView];
    
    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"jlai-cyds-museum02/jlai-cyds-museum02.theme"];
    [_mapView setThemeWithLocalPath:themePath];
    
    _isTotal = NO;
    // 设置当前楼层
    _currentGroup = @"1";
    // 设置地图显示的楼层数组 为["1"]; 即只有id为1的楼层显示
    _mapView.displayGroupIDs = @[_currentGroup];
    
    [self createButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createButton
{
    UIButton *groupsShowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    groupsShowBtn.frame = CGRectMake(0, SCREEN_HEIGHT-44, 80, 44);
    [groupsShowBtn setTitle:@"单层显示" forState:UIControlStateNormal];
    [groupsShowBtn setTitle:@"多层显示" forState:UIControlStateSelected];
    [groupsShowBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    groupsShowBtn.backgroundColor = [UIColor whiteColor];
    groupsShowBtn.layer.shadowColor = [UIColor grayColor].CGColor;
    groupsShowBtn.layer.shadowOpacity = 0.6;
    groupsShowBtn.layer.shadowOffset = CGSizeMake(1, 1);
    [groupsShowBtn addTarget:self action:@selector(groupsShowBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:groupsShowBtn];
    
    for (int i = 0; i < _mapView.map.groupNames.count; i++) {
        CGFloat groupBtnW = (SCREEN_WIDTH-80) / _mapView.map.groupNames.count;
        UIButton *groupBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        groupBtn.frame = CGRectMake(SCREEN_WIDTH-(i+1)*groupBtnW, SCREEN_HEIGHT-44, groupBtnW, 44);
        [groupBtn setTitle:((NSString *)_mapView.map.groupNames[i]).uppercaseString forState:UIControlStateNormal];
        [groupBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [groupBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [groupBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:groupBtn.frame.size] forState:UIControlStateNormal];
        [groupBtn setBackgroundImage:[UIImage imageWithColor:FMDemoColor size:groupBtn.frame.size] forState:UIControlStateSelected];
        groupBtn.layer.shadowColor = [UIColor grayColor].CGColor;
        groupBtn.layer.shadowOpacity = 0.6;
        groupBtn.layer.shadowOffset = CGSizeMake(1, 1);
        groupBtn.tag = ((NSString *)_mapView.map.groupIDs[i]).intValue;
        [groupBtn addTarget:self action:@selector(groupBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:groupBtn];
        
        if (i == 0) {
            groupBtn.selected = YES;
            _selectGroupBtn = groupBtn;
        }
    }
}

#pragma mark - FMKMapViewDelegate
- (void)mapViewDidFinishLoadingMap:(FMKMapView *)mapView {
    NSLog(@"地图加载完成");
}
- (void)mapViewDidFailLoadingMap:(FMKMapView *)mapView withError:(NSError *)error {
    NSLog(@"地图加载失败-%@", error);
}

#pragma mark - 单/多楼层切换按钮点击
- (void)groupsShowBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) { // 多层显示
        _isTotal = YES;
        // 设置楼层id数组为 所有楼层id（即多层显示）
        _mapView.displayGroupIDs = _mapView.map.groupIDs;
        // 设置焦点层为当前楼层 (焦点层不透明，非焦点层透明显示)； 可以选择切换时是否显示动画效果
        [_mapView setFocusByGroupID:_currentGroup animated:NO];
    }
    else { // 单层显示
        _isTotal = NO;
        // 设置楼层id数组为 当前楼层组成的数组
        _mapView.displayGroupIDs = @[_currentGroup];
    }
}

#pragma mark - 楼层选择按钮点击
- (void)groupBtnClick:(UIButton *)sender
{
    if (sender.tag == _selectGroupBtn.tag) return;
    
    _selectGroupBtn.selected = NO;
    sender.selected = YES;
    _selectGroupBtn = sender;
    
    _currentGroup = @(sender.tag).stringValue;
    if (_isTotal) {
        // 设置地图焦点层
        [_mapView setFocusByGroupID:_currentGroup animated:YES];
    }
    else {
        // 设置地图显示的楼层数组
        _mapView.displayGroupIDs = @[_currentGroup];
    }
}

@end
